home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- #define NOERR 0
- #define ERR 1
-
- char hexchar[17] = "0123456789ABCDEF";
-
- void hex2( char *p, int i )
- {
- *(p+0) = '0';
- *(p+1) = 'x';
- *(p+2) = hexchar[ ( i & 0xf0 ) >> 4 ];
- *(p+3) = hexchar[ ( i & 0x0f ) ];
- *(p+4) = 0;
-
- return;
- }
-
- int main( int argc, char *argv[] )
- {
- FILE *fin,*fout;
- int i,c;
- char buf[16];
-
- if( argc < 4 )
- {
- printf( "usage : dat2c [InputDataFile] [OutputC-SourceFile] [変数名]\n" );
- return ERR;
- }
-
- if( ( fin = fopen( argv[1], "rb" ) ) == NULL )
- {
- printf( "%s のオープンに失敗しました。\n", argv[1] );
- return ERR;
- }
-
- if( ( fout = fopen( argv[2], "w" ) ) == NULL )
- {
- printf( "%s のオープンに失敗しました。\n", argv[2] );
- return ERR;
- }
-
- fputs( "char\t", fout );
- fputs( argv[3], fout );
- fputs( " = {", fout );
-
- i = 0;
- while(1)
- {
- c = fgetc( fin );
- if( feof( fin ) )
- break;
- if( i )
- fputs( ",", fout );
- if( (i & 7) == 0 )
- fputs( "\n", fout );
- ++ i;
- hex2( buf, c );
- fputs( " ", fout );
- fputs( buf, fout );
- }
-
- fputs( "\n};\n", fout );
-
- fclose( fin );
- fclose( fout );
-
- return NOERR;
- }